<%@ page contentType="text/html;charset=UTF-8" %>
<%@include file="/WEB-INF/jsp/common/taglibs.jspf"%>
<div class="page-content">
    <div class="row">
        <div class="col-sm-12">
            <c:if test="${op eq '回复消息'}">
                <div class="control-group">
                    <span class="bold span2"><pretty:prettyTime date="${parent.sendDate}"/></span>
                    <c:choose>
                        <c:when test="${user.id eq parent.senderId}">我</c:when>
                        <c:otherwise>
                            <sys:showUsername id="${parent.senderId}" needLink="false"/>
                        </c:otherwise>
                    </c:choose>
                    发给
                    <c:choose>
                        <c:when test="${user.id eq parent.receiverId}">我</c:when>
                        <c:otherwise>
                            <sys:showUsername id="${parent.receiverId}" needLink="false"/>
                        </c:otherwise>
                    </c:choose>
                    的消息<br/>
                    <br/>
                    <div class="accordion message">
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle bold no-underline" data-toggle="collapse" href="#collapse${parent.id}">
                                        ${parent.title}
                                    <span class="muted" style="float: right;padding-right: 20px;">
                                        <sys:showUsername id="${parent.senderId}" needLink="false"/>
                                        &nbsp;&nbsp;&nbsp;
                                        <spring:eval expression="parent.sendDate"/>
                                    </span>
                                </a>
                            </div>
                            <div id="collapse${parent.id}" class="accordion-body collapse in">
                                <div class="accordion-inner">
                                        ${parent.content.content}
                                </div>
                            </div>
                    </div>
                </div>
                <br/>
            </c:if>
            <%--@elvariable id="m" type="com.bfl.sa.personal.message.entity.Message"--%>
            <form:form id="editForm" method="post" commandName="m" cssClass="form-horizontal">
                <es:BackURL hiddenInput="true"/>
                <es:showGlobalError commandName="m"/>
                <form:hidden path="id"/>
                <form:hidden path="content.id"/>
                <form:hidden path="parentId"/>
                <form:hidden path="parentIds"/>

                <c:if test="${op eq '发送新消息' or op eq '转发消息' or op eq '发送草稿'}">
                <div class="form-group">
                    <form:label path="receiverId" cssClass="col-sm-2 control-label no-padding-right">收件人</form:label>
                    <div id="search-receiver" class="col-sm-10">
                        <c:set var="receiver" value="${not empty receiver ? receiver : param.receiver}"/>
                        <input type="text" id="receiverId_msg" class="form-control" name="receiver" value="${receiver}" placeholder="输入收件人用户名">
                        <form:hidden path="receiverId"/>
                    </div>
                </div>
                </c:if>
                <div class="form-group">
                    <form:label path="title" cssClass="col-sm-2 control-label no-padding-right">标题</form:label>
                    <div class="col-sm-10">
                        <form:input path="title" cssClass="form-control validate[required,minSize[5],maxSize[200]]" placeholder="长度在5-200个字符之间"/>
                    </div>
                </div>
                <div class="form-group">
                    <form:label path="content.content" cssClass="col-sm-2 control-label no-padding-right">内容</form:label>
                    <div class="col-sm-10 textarea">
                        <form:textarea id="content" path="content.content" cssClass="form-control" cssStyle="height: 250px;"/>
                    </div>
                </div>
                <div class="btn-group-sm center">
                    <button id="btnSave" class="btn btn-info" type="button">
                        <i class="ace-icon fa fa-check"></i> 确定
                    </button>&nbsp; &nbsp; &nbsp;
                    <button class="btn" type="button" onclick="$.app.closeDialog()">
                        <i class="ace-icon fa fa-undo"></i> 取消
                    </button>
                </div>
            </form:form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        var form = $("#editForm");
        var validationEngine = form.validationEngine({prettySelect:true, useSuffix : "_msg"});
        <es:showFieldError commandName="m"/>

        var editor = KindEditor.create("textarea[name='content.content']", {
            themeType: 'simple',
            uploadJson: '${ctx}/kindeditor/upload',
            fileManagerJson: '${ctx}/kindeditor/filemanager',
            allowFileManager: true,
            afterBlur: function(){this.sync();}
        });

        var $username = $("#receiverId_msg");
        if($username[0]){
            $.app.initAutocomplete({
                input : $username,
                source : "${ctx}/admin/sys/user/ajax/autocomplete",
                select : function(event, ui) {
                    $username.val(ui.item.label);
                    return false;
                }
            });
        }

        $(window).on('beforeunload',function() {
            if($username.val() || $("#title").val() || editor.html()) {
                return "确定离开当前编辑页面吗？";
            }
        });

        form.submit(function() {
            $(window).unbind("beforeunload");
        });

        $(".btn-save-draft").click(function() {
            //必须调用 尽管 form.submit绑定了释放，但是 form.validationEngine("detach"); 会off掉
            $(window).unbind("beforeunload");
            form.validationEngine("detach");
            form.attr("action", "${ctx}/admin/personal/message/draft/save").submit();
        });
    });
</script>
